package com.midea.ai.air.ui.more.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.more.data.MainListItem;

import java.util.ArrayList;

/**
 * Created by ouyangdi on 2015/12/8.
 * "更多"主页，ListView的适配器(adapter)
 */
public class MainListAdapter extends ArrayAdapter<MainListItem> {
    Context mContext;

    public MainListAdapter(Context context, ArrayList<MainListItem> mainListItems) {
        super(context, 0, mainListItems);
        this.mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_more,null);
            holder= new ViewHolder();
            holder.ivIcon = (ImageView) convertView.findViewById(R.id.imgView_icon);
            holder.tvName = (TextView) convertView.findViewById(R.id.txtView_name);
            holder.botLine = convertView.findViewById(R.id.bottom_line);
            holder.topLine = convertView.findViewById(R.id.top_line);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        MainListItem item = getItem(position);

        holder.ivIcon.setImageResource(item.getResIdIcon());
        holder.ivIcon.setVisibility(View.VISIBLE);

        holder.tvName.setText(item.getStrName());

        if(item.isDrawTopLine()) {
            holder.topLine.setVisibility(View.VISIBLE);
        } else if(!item.isDrawTopLine()) {
            holder.topLine.setVisibility(View.INVISIBLE);
        }

        if(item.isDrawBotLine()) {
           holder.botLine.setVisibility(View.VISIBLE);
        } else if(!item.isDrawBotLine()) {
           holder.botLine.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    public class ViewHolder {
        public ImageView ivIcon;
        public TextView tvName;
        public View botLine;
        public View topLine;
    }
}
